import type { VipIconThemeConfig, VipIconThemeType } from '../types/interfaces'
import { IconService } from '..'
import { IconFormatType } from '../../../types/interfaces'
import { ToolKit } from '../../utils/toolkit'
import { VIP_ICON_THEME_COLORS, VIP_ICONS } from '../types/constants'

export class VipIconService {
  static iconService: IconService = IconService.getInstance()

  /**
   * 获取 VIP 图标（支持多主题）
   * @param level VIP等级 (1-10)
   * @param theme 主题类型
   * @param size 图标尺寸
   * @param format 输出格式
   */
  static getVipIcon(
    level: number,
    theme: VipIconThemeType = 'gold',
    size: number = 18,
    format: IconFormatType = IconFormatType.dataUrl,
  ): string {
    const validatedLevel = Math.max(1, Math.min(Object.keys(VIP_ICONS).length, Math.round(level)))
    const colors = VIP_ICON_THEME_COLORS[theme]

    // 获取基础 SVG
    const svgCode = Object.values(VIP_ICONS)[validatedLevel - 1] || VIP_ICONS.LEVEL_ONE

    // 应用主题色
    const themedSvg = this.applyThemeColors(svgCode, colors)

    // 调整尺寸
    const sizedSvg = themedSvg
      .replace(/width="\d+"/, `width="${size}"`)
      .replace(/height="\d+"/, `height="${size}"`)

    return ToolKit.ui.svg.formatSvgIcon(sizedSvg, format)
  }

  /**
   * 生成VIP图标的<img>标签
   * @param level VIP等级
   * @param options 配置项
   * @param options.theme 主题类型
   * @param options.size 图标尺寸
   * @param options.alt 图片 alt 属性
   * @param options.className 图片 class 属性
   * @param options.attributes 其他属性
   */
  static createVipImage(
    level: number,
    options: {
      theme?: VipIconThemeType
      size?: number
      alt?: string
      className?: string
      attributes?: Record<string, string>
    } = {},
  ): string {
    const {
      theme = 'gold',
      size = 18,
      alt = `VIP ${level}`,
      className = 'custom-vip-icon',
      attributes = {},
    } = options

    const svg = this.getVipIcon(level, theme, size, IconFormatType.svg)

    return ToolKit.ui.svg.createSvgImage(svg, alt, className, {
      width: String(size),
      height: String(size),
      ...attributes,
    })
  }

  private static applyThemeColors(svg: string, config: VipIconThemeConfig): string {
    return svg
      .replace(/\{\{right\}\}/g, config.right)
      .replace(/\{\{left\}\}/g, config.left)
      .replace(/\{\{text\}\}/g, config.text)
  }

  /**
   * 生成 VIP 图标样式
   */
  styles(): string {
    return ``
  }
}
